package com.cloud.charts4a.demo;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.cloud.charts4a.chart.TimeChart;
import com.cloud.charts4a.chart.TimeChart.IAfterDragChartListener;
import com.cloud.charts4a.chart.TimeChart.XAxisAttribs;
import com.cloud.charts4a.chart.attrib.YAxisAttribs;
import com.cloud.charts4a.chart.attrib.YRangeHighLightAttribs;
import com.cloud.charts4a.data.ChartDataSeries;
import com.cloud.charts4a.data.TimeChartData;
import com.cloud.charts4a.util.MathUtil;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ToggleButton;

/**
 * Time Chart Demo</p>
 * 
 * @version
 * <ol>
 * 	<li>2010/07/09 CloudTu：First Release</li>
 * </ol>  
 * 
 * @author cloudtu
 */
public class TimeChartDemo extends AbstractChartDemo {
	private TimeChart timeChart;
	private Date minXdate;
	private Date maxXdate;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.time_chart);   
		
        timeChart = (TimeChart)findViewById(R.id.timeChart);        
    
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -3);
        minXdate = calendar.getTime();
        calendar.add(Calendar.DATE, 7);
        maxXdate = calendar.getTime();
        initTimeChartView(minXdate, maxXdate);		
    }    
    
    @Override
    protected Dialog createChartConfigDialog(){
    	LayoutInflater layoutInflater = LayoutInflater.from(this);
    	View chartConfigView = layoutInflater.inflate(R.layout.chart_config, null);
        
    	final ToggleButton showXUnitDescButton = (ToggleButton)chartConfigView.findViewById(R.id.showXUnitDescButton);
        showXUnitDescButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				timeChart.setShowXUnitDesc(showXUnitDescButton.isChecked());
			}
		});        
        
        final ToggleButton showYUnitDescButton = (ToggleButton)chartConfigView.findViewById(R.id.showYUnitDescButton);
        showYUnitDescButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				timeChart.setShowYUnitDesc(showYUnitDescButton.isChecked());
			}
		});        
        
        final ToggleButton showLegendButton = (ToggleButton)chartConfigView.findViewById(R.id.showLegendButton);
        showLegendButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				timeChart.setShowLegend(showLegendButton.isChecked());
			}
		});   
        
        final ToggleButton onlyShowDotButton = (ToggleButton)chartConfigView.findViewById(R.id.onlyShowDotButton);
        onlyShowDotButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				timeChart.setOnlyShowDot(onlyShowDotButton.isChecked());
			}
		});          
        
        final ToggleButton fixedYAxisRangeButton = (ToggleButton)chartConfigView.findViewById(R.id.fixedYAxisRangeButton);
        fixedYAxisRangeButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				if(fixedYAxisRangeButton.isChecked()){
			    	XAxisAttribs xAxisAttribs = new XAxisAttribs(minXdate,maxXdate,"MM/dd",2*24*60,getString(R.string.date));	        
			    	YAxisAttribs yAxisAttribs = new YAxisAttribs(0,150,30, getString(R.string.amount));		
					timeChart.initChartBackground(xAxisAttribs, yAxisAttribs);					
				}
				else{
			    	XAxisAttribs xAxisAttribs = new XAxisAttribs(minXdate,maxXdate,"MM/dd",2*24*60,getString(R.string.date));	        
			    	YAxisAttribs yAxisAttribs = new YAxisAttribs(20, getString(R.string.amount));		
					timeChart.initChartBackground(xAxisAttribs, yAxisAttribs);					
				}
			}
		});          
        
        final ToggleButton setYRangeHighLightAttribsButton = (ToggleButton)chartConfigView.findViewById(R.id.setYRangeHighLightAttribsButton);
        setYRangeHighLightAttribsButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				if(setYRangeHighLightAttribsButton.isChecked()){
					timeChart.setYRangeHighLightAttribs(new YRangeHighLightAttribs(80,110,Color.argb(128, 153, 102, 51)));							
				}
				else{
					timeChart.setYRangeHighLightAttribs(null);							
				}
			}
		});  
        
        final ToggleButton setAfterDragChartListenerButton = (ToggleButton)chartConfigView.findViewById(R.id.setAfterDragChartListenerButton);
        setAfterDragChartListenerButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				if(setAfterDragChartListenerButton.isChecked()){
					timeChart.setAfterDragChartListener(new IAfterDragChartListener() {
						@Override
						public List<ChartDataSeries<TimeChartData>> reCreateChartContent(Date minXDate, Date maxXDate) {
							java.sql.Date minXAxisDate = new java.sql.Date(minXDate.getTime());
							java.sql.Date maxXAxisDate = new java.sql.Date(maxXDate.getTime());
							return createMockTimeChartData(minXAxisDate,maxXAxisDate);
						}		
					});						
				}
				else{
					timeChart.setAfterDragChartListener(null);
				}
			}
		});        
        
    	return new AlertDialog.Builder(TimeChartDemo.this)
			.setTitle(R.string.chartConfig)
			.setView(chartConfigView)
			.setPositiveButton(R.string.ok,new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,int whichButton) {			        
			        timeChart.drawChart();
				}
			})
			.create();
    }
    
    private void initTimeChartView(Date minXdate, Date maxXdate){		
    	XAxisAttribs xAxisAttribs = new XAxisAttribs(minXdate,maxXdate,"MM/dd",2*24*60,getString(R.string.date));	        
    	YAxisAttribs yAxisAttribs = new YAxisAttribs(20, getString(R.string.amount));		
		timeChart.initChartBackground(xAxisAttribs, yAxisAttribs);
		
		List<ChartDataSeries<TimeChartData>> timeChartDataSeriesList = createMockTimeChartData(minXdate,maxXdate); 
		timeChart.setChartContent(timeChartDataSeriesList);										
		
		timeChart.setTipDateFormat("MM/dd HH:mm");
		timeChart.drawChart();
    }
    
    private List<ChartDataSeries<TimeChartData>> createMockTimeChartData(Date minXDate,Date maxXDate){				
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(minXDate);
		calendar.set(Calendar.HOUR_OF_DAY, 12);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		minXDate = calendar.getTime();
		
		calendar.setTime(maxXDate);
		calendar.set(Calendar.HOUR_OF_DAY, 12);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);		
		maxXDate = calendar.getTime();	    	
    	
    	List<ChartDataSeries<TimeChartData>> timeChartDataSeriesList = new ArrayList<ChartDataSeries<TimeChartData>>();

    	int[] colors = new int[]{Color.RED,Color.GREEN,Color.BLUE};
    	long oneDayAsMillisecs = 24 * 60 * 60 * 1000;
    	
    	for(int i=1;i<=3;i++){    		
    		ChartDataSeries<TimeChartData> timeChartDataSeries = new ChartDataSeries<TimeChartData>(getString(R.string.test) + i, colors[i-1]);	    		    		
    		long j = minXDate.getTime();
    		while(j <= maxXDate.getTime()){    			
    			timeChartDataSeries.addChartData(new TimeChartData(new Date(j), MathUtil.createRandomValue(50*i-10,50*i+10)));
    			j = j + oneDayAsMillisecs;
    		}   
    		timeChartDataSeriesList.add(timeChartDataSeries);    		
    	}
    	
		return timeChartDataSeriesList;
	}    
}